<?php

namespace App\Common\Middleware\SysMiddleware;


use App\Extend\SysExtend\Cache\Cache;
use App\Extend\SysExtend\Middleware\MiddlewareInterface;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;

class SessionMiddleware implements MiddlewareInterface
{

    /**
     * 控制方法执行前执行的方法
     * @param Request $request
     * @param Response $response
     * @return mixed
     */
    public function before(Request $request, Response $response)
    {
        // TODO: Implement before() method.
        // 检查cookie是否含有session
        $session_id = $request->getCookieParams('SESSIONID');
        if(!$session_id) {
            $session_id = md5(time() . mt_rand(1000,9999) . 'session_key');
            $response->setCookie('SESSIONID', $session_id);
            Cache::set($session_id,[],24*3600);
        }
    }

    /**
     * 控制方法执行后执行的方法
     * @param Request $request
     * @param Response $response
     * @return mixed
     */
    public function after(Request $request, Response $response)
    {
        // TODO: Implement after() method.
    }
}